<template>
  <div class="dataExchangeContainer" ref="dataExchangeContainer">
    <div class="dataExchangeBox clearfix w1200">
      <div class="leftBox">
        <h1>{{ info.title }}</h1>
        <div class="userAndBtn flex align-center justify-between">
          <div class="userBox flex align-center">
            <img src="@/assets/images/portalWebsite/headimg.png" alt="">
            <span>{{ info.examineUserName ? info.examineUserName : '暂无' }}</span>
          </div>
          <el-button type="primary" round size="mini" @click="dialogVisible = true">申请</el-button>
        </div>
        <div class="contentBox">
          <div class="remarkBox" v-html="info.remark">
          </div>
          <div class="content" v-html="info.content"></div>
        </div>
      </div>
      <div class="rightBox" :class="{fixed}">
        <div class="attachmentListBox">
          <h2>相关附件</h2>
          <div class="attachmentList">
            <Accessory :list="info.attachment"/>
            <div class="noDataBox flex align-center justify-center"
                 v-if="info.attachment && info.attachment.length === 0">
              <el-empty description="暂无附件信息"></el-empty>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      title="申请"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <div class="requestReasonBox">
        <el-input
          type="textarea"
          :rows="3"
          placeholder="输入您的申请原因..."
          maxlength="100"
          show-word-limit
          v-model="requestReason">
        </el-input>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" icon="el-icon-close">取 消</el-button>
        <el-button type="primary" @click="exchangeUseHandler" icon="el-icon-check">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import {getExchangeDetail, exchangeUse} from '@/api/compre/exchange'
import Accessory from './model/Accessory';

export default {
  name: '',
  props: {},
  components: {
    Accessory
  },
  data() {
    return {
      appWrapper: null,
      fixed: false,
      info: {},
      dialogVisible: false,
      requestReason: ''
    }
  },
  computed: {},
  watch: {},
  created() {
    let id = this.$route.params.id;
    this.getDetail(id);
  },
  mounted() {
    this.$nextTick(() => {
      this.appWrapper = this.$refs.dataExchangeContainer;
      this.appWrapper.addEventListener("scroll", this.scrollFixedHeader, true);
    })
  },
  destroyed() {
    this.appWrapper.removeEventListener("scroll", this.scrollFixedHeader, true);
  },
  methods: {
    scrollFixedHeader(e) {
      let scrollTop = this.appWrapper.scrollTop;
      this.fixed = scrollTop > 106;
    },
    async getDetail(id) {
      let {code, data} = await getExchangeDetail(id);
      if (code === 200) {
        let attachmentList = data.attachment.split(',');
        let attachmentArr = [];
        if (attachmentList && attachmentList.length > 0) {
          attachmentList.forEach((item, index) => {
            let itemArr = item.split('/');
            attachmentArr.push({
              name: itemArr[itemArr.length - 1],
              url: item
            })
          })
          data.attachment = attachmentArr;
        } else {
          data.attachment = [];
        }
        this.info = data;
      }
    },
    async exchangeUseHandler() {
      if (this.requestReason === '') {
        this.$message.error('请输入您的申请理由');
        return;
      }
      let {code, data, msg} = await exchangeUse({
        exchangeId: this.info.id,
        requestReason: this.requestReason
      });
      if (code === 200) {
        this.$message({
          message: '申请成功',
          type: 'success'
        });
        this.dialogVisible = false;
      } else {
        this.$message({
          message: msg,
          type: 'error'
        });
        this.dialogVisible = false;
      }
    }
  },
}
</script>
<style lang="scss" scoped>
.w1200 {
  width: 1200px;
  margin: 0 auto;
}
::v-deep .el-dialog{
  width: 500px !important;
}
@mixin scrollBar {
  &::-webkit-scrollbar-track-piece {
    background: #d3dce6;
  }

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-thumb {
    background: #99a9bf;
    border-radius: 20px;
  }
}

.dataExchangeContainer {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  overflow-x: hidden;
  @include scrollBar;

  .dataExchangeBox {
    .leftBox {
      width: 824px;
      float: left;
      margin: 22px 0;
      box-shadow: 0 8px 16px 0 rgba(7, 17, 27, .05);
      border-radius: 12px;
      box-sizing: border-box;
      padding: 40px 40px 24px 40px;
      background: #fff;

      > h1 {
        font-size: 32px;
        color: #1c1f21;
        line-height: 40px;
        margin: 8px 0 20px;
        word-wrap: break-word;
        word-break: break-all;
      }

      .userAndBtn {
        height: 30px;

        .userBox {
          img {
            width: 30px;
            height: 30px;
            display: block;
            margin-right: 10px;
          }

          span {
            font-size: 14px;
          }
        }
      }

      .remarkBox {
        background-color: #F5F8F4;
        padding: 20px;
        margin: 30px 0;
        color: #333333;
        border-radius: 4px;
        font-size: 14px;
        line-height: 22px;
        text-indent: 2em;
      }
    }

    .rightBox {
      width: 356px;
      float: right;
      overflow-x: hidden;
      margin-top: 22px;
      box-shadow: 0 8px 16px 0 rgba(7, 17, 27, .05);
      border-radius: 12px;
      box-sizing: border-box;
      padding: 0 24px;
      @include scrollBar;
      background: #fff;

      &.fixed {
        position: fixed;
        top: 84px;
        margin-left: 844px;
      }
    }
  }
}

.attachmentListBox {
  padding-bottom: 24px;
  padding-top: 24px;

  > h2 {
    font-size: 16px;
    color: #07111b;
    font-weight: 700;
    margin: 0 0 24px;
  }
}
</style>
